/*
 * 自己的JS脚步
 * @Author: yanxu
 * @Date:   2016-09-08 10:22:26
 */

'use strict';

ImporInfor();
$('.supply-info>.modal-dialog>.modal-content .uploadImg').css('height', $('.supply-info>.modal-dialog>.modal-content .uploadImg'))
var supplyPhoto=[];
var purchasingPhoto=[];

$(".nav-tabs > li > a").click(function(){
    $(".nav-tabs > li > a").removeAttr("class");
});

filldate();


defaultTab(getQueryString('message'));
//默认的tab选项卡
function defaultTab(param){
    if(param=='purchasing'){
//          $(".nav-tabs > li > a").removeAttr("class");
        $('.nav-tabs a[href="#purchasing-information"]').tab('show');
    }else {
        $('.nav-tabs a[href="#supply-information"]').tab('show');
    }


}

// 填充默认的数据
function filldate(){

    var sellerPhone=Cookie.getCookie("phone");
    var sellerUsername=Cookie.getCookie("username");
    var sellerWechat=Cookie.getCookie("wechat");
    var sellerqq=Cookie.getCookie("qq");
   

    if(sellerPhone==undefined||sellerPhone==""||sellerPhone instanceof Object ){
        sellerPhone=""
    }
    if(sellerUsername==undefined||sellerUsername==""||sellerUsername instanceof Object){
        sellerUsername=""
    }
    if(sellerWechat==undefined||sellerWechat==""||sellerWechat instanceof Object){
        sellerWechat=""
    }
    if(sellerqq==undefined||sellerqq==""||sellerqq instanceof Object){
        sellerqq=""
    }

    $("#supply-information .seller-information .name").val(sellerUsername);
    $("#supply-information .seller-information .phone").val(sellerPhone);
    $("#supply-information .seller-information .QQ").val(sellerqq);
    $("#supply-information .seller-information .WeChat").val(sellerWechat);
    $("#purchasing-information .seller-information .name").val(sellerUsername);
    $("#purchasing-information .seller-information .phone").val(sellerPhone);
    $("#purchasing-information .seller-information .QQ").val(sellerqq);
    $("#purchasing-information .seller-information .WeChat").val(sellerWechat);

}

// 填充默认的数据结束


//控制保质期的值

/*$('#information>.tab-content>.tab-pane>div>.row .quality-guarantee-period').bind('input propertychange', function() {
    if($(this).val()>7){
        $(this).val(7)
    }
});*/

//供应展开收起的按钮
$('#information>.tab-content>#supply-information>.fill-information>.hidden-btn').click(function(){
    if($('#information>.tab-content>#supply-information>.fill-information>.is-show').hasClass("hidden")){
        $('#information>.tab-content>#supply-information>.fill-information>.is-show').removeClass("hidden");

        $('#information>.tab-content>#supply-information>.fill-information>.hidden-btn img').attr("src","./img/icon_top.png");
    }else{
        $('#information>.tab-content>#supply-information>.fill-information>.is-show').addClass("hidden");
        $('#information>.tab-content>#supply-information>.fill-information>.hidden-btn img').attr("src","./img/icon_bottom.png");
    }
});


//采购展开收起的按钮
$('#information>.tab-content>#purchasing-information>.fill-information>.hidden-btn').click(function(){
    if($('#information>.tab-content>#purchasing-information>.fill-information>.is-show').hasClass("hidden")){
        $('#information>.tab-content>#purchasing-information>.fill-information>.is-show').removeClass("hidden");

       $('#information>.tab-content>#purchasing-information>.fill-information>.hidden-btn img').attr("src","./img/icon_top.png");
    }else{
        $('#information>.tab-content>#purchasing-information>.fill-information>.is-show').addClass("hidden");
        $('#information>.tab-content>#purchasing-information>.fill-information>.hidden-btn img').attr("src","./img/icon_bottom.png");
    }
});

//让高度等于宽度  结束



//弹出框开始

//填充供货类型数据
var provideType="/provideWay/getProvideWayInfos";
askprovideType(provideType);

//填充肉的类别
var packing="/packing/getPackingInfos";
askpacking(packing);


//填充包装规格
// var meatCategory="/category/getMeatCategoryInfos";
// getMeatCategoryInfos(meatCategory);

//填充肉的种类
var meatCategory="/category/getMeatCategoryInfos";
getSupplyMeatCategoryInfos(meatCategory+'?meatType=0');
getPurchasingMeatCategoryInfos(meatCategory+'?meatType=1');



//填充产地
var providePlace="/country/getCountryInfos";
getProvidePlace(providePlace);

//弹出框结束
$(".modal>.modal-dialog>.modal-content>.cancel").click(function(){
    $('.supply-meat-type').modal('hide');
    $('.supply-type').modal('hide');
    $('.purchasing-meat-type').modal('hide');
    $('.supply-packing').modal('hide');
    $('.supply-meat-category').modal('hide');
    $('.supply-place').modal('hide');
    $('.purchasing-place').modal('hide');
    $('.purchasing-meat-category').modal('hide');
});

$('.supply-meat-type,.supply-type,.purchasing-meat-type,.supply-packing,.supply-meat-category,.supply-place,.purchasing-place,.purchasing-meat-category').on('shown.bs.modal', function (e) {
   /* $(".tab-pane").bind("touchmove",function(event){
        event.preventDefault();
    });
    $(".tab-pane").addClass(".modal-open")*/


});
$('.supply-meat-type,.supply-type,.purchasing-meat-type,.supply-packing,.supply-meat-category,.supply-place,.purchasing-place,.purchasing-meat-category').on('hidden.bs.modal', function (e) {

   /* $(".tab-pane").unbind("touchmove");*/

});




//供应选择肉的种类
$(".supply-meat-type>.modal-dialog>.modal-content>.option-content>div").click(function(){
//	alert(1)
//	$('#supply-information meat-category-input').val('')
    $("#supply-information>.fill-information .row .meat-type-input").val($(this).html());
    $("#supply-information>.fill-information .row .meat-category-input").val('');
    $("#supply-information>.fill-information .row .meat-type-input").data("value",$(this).data("value"));
    $('.supply-meat-type').modal('hide');

    getSupplyMeatCategoryInfos(meatCategory+'?meatType='+$(this).data("value"));




});
//供应选择肉的种类结束



//供应选择肉的种类结束

//采购选择肉的种类
$(".purchasing-meat-type>.modal-dialog>.modal-content>.option-content>div").click(function(){

    $("#purchasing-information>.fill-information .row .meat-type-input").val($(this).html());
    $("#purchasing-information>.fill-information .row .meat-type-input").data("value",$(this).data("value"));
    $('.purchasing-meat-type').modal('hide');

    getPurchasingMeatCategoryInfos(meatCategory+'?meatType='+$(this).data("value"));


})
//采购选择肉的种类结束



//供应详情确定按钮开始
// $(".supply-info .footer-nav>button").click(function(){
//     var supplyInfoTxt =  $(".supply-info .option-content>.txt>.content").val();
//
//     $("#supply-information>.fill-information .row .provide-info-input ").val(supplyInfoTxt);
//     $("#supply-information>.fill-information .row .provide-info-input ").data("image",supplyPhoto);
//
//     $('.supply-info').modal('hide');
// })
//供应详情确定按钮结束

//采购详情确定按钮开始

$(".purchasing-info .footer-nav>button").click(function(){
	

    var purchasingInfoTxt =  $(".purchasing-info .option-content>.txt>.content").val();
    
   	
    $("#purchasing-information>.fill-information .row .purchasing-info-input").val(purchasingInfoTxt);
    $("#purchasing-information>.fill-information .row .purchasing-info-input").data("image",purchasingPhoto);

    $('.purchasing-info').modal('hide');
})
//采购详情确定按钮结束



// 发布信息供货页面
var urlProvide = '/myhome/provide/addProvideInfo';
var urlpurchasing = '/myhome/purchaseInfo/addPurchaseInfo';
var uuidLocal = Cookie.getCookie('uuid');

$('#footer-btn button').eq(1).on('click',function(){
//	alert(1)
	
	if($(".nav-tabs > li:eq(0)").hasClass("active")){
		console.log(uuidLocal)
        addProvideInfo(urlProvide,uuidLocal);
    }else{
        addPurchasingInfo(urlpurchasing,uuidLocal);
    }
});





	


$('#footer-btn button').eq(0).on('click',function(){
	
	
	layer.open({
        content: '您确定要清空输入内容？'
        ,btn: ['取消', '确认']
        ,style: 'color:#333;'
        ,skin: 'footer'
        ,no: function(){
        	fn()
            layer.closeAll();
            filldate();
//          count = $(target).data('img');
//          imgList.splice(count,1)
//          supplyPhoto.splice(count,1)
//          $(target).parent('span').fadeOut(500,function () {
//              if(imgList.length < 5) {
//                  $upInput.show();
//              }
//              $(this).remove()
//              $('.img-container').find('i').each(function () {
//                  $(this).attr('data-img',$(this).index('i'))
//              })
//          })
        }
    });
	
    function fn(){
    	if($(".nav-tabs > li:eq(0)").hasClass("active")){
	        deleteProvideInfo();
	    }else{
	        deletePurchasingInfo();
	    }
    }
   
})

//请求数据     post请求方式  //这是需要清空页面ajax
function addProvideInfo(num,uuid){
    var parm="";

    if(uuid=="undefined"||uuid==null||uuid==""){
        alert("你还没有登陆");
        return;
    }
//  parm=parm+"uuid="+uuid;
    //获取肉的种类
var meatTypeInput =$("#information #supply-information .fill-information .meat-type-input").data("value");
    if(meatTypeInput=="undefined"||meatTypeInput==null||meatTypeInput===""){
        alert("肉的种类不可以为空");
        return;
    }
    if(meatTypeInput!="undefined"&&meatTypeInput!=""&&meatTypeInput!=null) {
        parm = parm + "&meatType=" + meatTypeInput;
    }

    //获取供应详情
var provideInfoInput =$("#information #supply-information .fill-information .provide-info-input").val();

    if(provideInfoInput=="undefined"||provideInfoInput==null||provideInfoInput===""){
        alert("供应详情不可以为空");
        return;
    }

    if(provideInfoInput!="undefined"&&provideInfoInput!=""&&provideInfoInput!=null) {
        parm = parm + "&provideDesc=" + encodeURIComponent(provideInfoInput);
    }

    //获取供应详情图片
var provideImage =$("#information #supply-information .fill-information .provide-info-input").data("image");

    if(provideImage!="undefined"&&provideImage!=""&&provideImage!=null) {
        if(typeof provideImage === 'string'){
            parm = parm + "&imageUrl=" + provideImage;
        }else{
            parm = parm + "&imageUrl=" + provideImage.join(",");
        }

    }

    //获取肉的类别
var meatCategoryInput =$("#information #supply-information .fill-information .meat-category-input").data("value");
    if(meatCategoryInput!="undefined"&&meatCategoryInput!=""&&meatCategoryInput!=null) {
        parm = parm + "&meatCategoryId=" + meatCategoryInput;
    }

    //获取肉数量
var meatNumInput =$("#information #supply-information .fill-information .meat-num-input").val();
    if(meatNumInput!="undefined"&&meatNumInput!=""&&meatNumInput!=null) {
        parm = parm + "&amount=" + meatNumInput;
    }

    //获取肉单位
var meatunitInput =$("#information #supply-information .fill-information .meat-unit-input").val();
    if(meatunitInput!="undefined"&&meatunitInput!=""&&meatunitInput!=null) {
        parm = parm + "&unit=" + meatunitInput;
    }
    
    //获取单价
var meatPriceInput =$("#information #supply-information .fill-information .meat-price-input").val();
    if(meatPriceInput!="undefined"&&meatPriceInput!=""&&meatPriceInput!=null) {
        parm = parm + "&price=" + meatPriceInput;
    }

    //获取产地
var supplyPlaceInput =$("#information #supply-information .fill-information .supply-place-input").data("value");
    if(supplyPlaceInput!="undefined"&&supplyPlaceInput!=""&&supplyPlaceInput!=null) {
        parm = parm + "&countryId=" + supplyPlaceInput;
    }

    //获取包装规格
var supplyPackingInput =$("#information #supply-information .fill-information .supply-packing-input").data("value");
    if(supplyPackingInput!="undefined"&&supplyPackingInput!=""&&supplyPackingInput!=null) {
        parm = parm + "&packingId=" + supplyPackingInput;
    }

    //获取供应类型
var supplyTypeInput =$("#information #supply-information .fill-information .supply-type-input").data("value");
    if(supplyTypeInput!="undefined"&&supplyTypeInput!=""&&supplyTypeInput!=null) {
        parm = parm + "&provideWayId=" + supplyTypeInput;
    }
    //获取保质期
var qualityGuaranteePeriod =$("#information #supply-information .fill-information .quality-guarantee-period").val();
    if(qualityGuaranteePeriod!="undefined"&&qualityGuaranteePeriod!=""&&qualityGuaranteePeriod!=null) {
        parm = parm + "&shelfLife=" + qualityGuaranteePeriod;
    }
    //获取精修比例
var scaleInput =$("#information #supply-information .fill-information .scale-input").val();
    if(scaleInput!="undefined"&&scaleInput!=""&&scaleInput!=null) {
        parm = parm + "&scale=" + scaleInput;
    }
    //获取工厂编号
var factoryNumInput =$("#information #supply-information .fill-information .factory-num-input").val();
    if(factoryNumInput!="undefined"&&factoryNumInput!=""&&factoryNumInput!=null) {
        parm = parm + "&factoryNo=" + factoryNumInput;
    }

    //获取失效期
var failurePeriodInput =$("#information #supply-information .fill-information .failure-period-input").val();
    if(failurePeriodInput!="undefined"&&failurePeriodInput!=""&&failurePeriodInput!=null) {
        parm = parm + "&invalidDate=" + failurePeriodInput;
    }

    //获取姓名
var name =$("#information #supply-information .seller-information .name").val();
    if(name!="undefined"&&name!=""&&name!=null) {
        parm = parm + "&contact=" + name;
    }

    //获取手机号
var phone =$("#information #supply-information .seller-information .phone").val();
    if(phone!="undefined"&&phone!=""&&phone!=null) {
        parm = parm + "&phone=" + phone;
    }

    //获取QQ
 var QQ =$("#information #supply-information .seller-information .QQ").val();
        if(QQ!="undefined"&&QQ!=""&&QQ!=null) {
            parm = parm + "&qq=" + QQ;
        }
    //获取微信
    var weChat =$("#information #supply-information .seller-information .WeChat").val();
            if(weChat!="undefined"&&weChat!=""&&weChat!=null) {
                parm = parm + "&weChat=" + weChat;
            }
    parm=parm +"&" + Infornation;
    
    console.log(parm)
	// $.ajax({
	// 	url:host + num,
	// 	data:parm,
     //    type:"get",
	// 	success:function(res){
	// 		console.log(res)
	// 		if(res.success == true){
     //            self.location="supplyMessage.html";
     //            deleteProvideInfo();
     //       }else{
	// 			alert(res.message)
	// 		}
	// 	},
     //    error:function () {
     //        return null;
     //    }
	// });
}

function deleteProvideInfo(){
   $("#information #supply-information .fill-information .meat-type-input").data("value","");
    $("#information #supply-information .fill-information .meat-type-input").val("");

    //获取供应详情
    $("#information #supply-information .fill-information .provide-info-input").val("");
    $(".supply-info .option-content>.txt>.content").val("");

    //获取肉的类别
   $("#information #supply-information .fill-information .meat-category-input").data("value","");
    $("#information #supply-information .fill-information .meat-category-input").val("");

    //获取肉数量
   $("#information #supply-information .fill-information .meat-num-input").val("");

    //获取肉单位
    $("#information #supply-information .fill-information .meat-unit-input").val("");
    
    
    //获取肉的价格
    $("#information #supply-information .fill-information .meat-price-input").val("");

    //获取产地
    $("#information #supply-information .fill-information .supply-place-input").data("value","");
    $("#information #supply-information .fill-information .supply-place-input").val("");

    //获取包装规格
    $("#information #supply-information .fill-information .supply-packing-input").data("value","");
    $("#information #supply-information .fill-information .supply-packing-input").val("");

    //获取供应类型
    $("#information #supply-information .fill-information .supply-type-input").data("value","");
    $("#information #supply-information .fill-information .supply-type-input").val("");

    //获取保质期
    $("#information #supply-information .fill-information .quality-guarantee-period").val("");

    //获取精修比例
    $("#information #supply-information .fill-information .scale-input").val("");

    //获取工厂编号
    $("#information #supply-information .fill-information .factory-num-input").val("");

    //获取失效期
    $("#information #supply-information .fill-information .failure-period-input").val("");

    //获取姓名
    $("#information #supply-information .seller-information .name").val("");

    //获取手机号
    $("#information #supply-information .seller-information .phone").val("");

    //获取QQ
    $("#information #supply-information .seller-information .QQ").val("");

    //获取微信
    $("#information #supply-information .seller-information .WeChat").val("");

}



function addPurchasingInfo(num,uuid){
    var parm="";
//  parm=parm+"uuid="+uuid;

    if(uuid=="undefined"||uuid==null||uuid==""){
        alert("你还没有登陆");
        return;
    }

    //获取肉的类别
    var meatTypeInput =$("#information #purchasing-information .fill-information .meat-type-input").data("value");

    if(meatTypeInput=="undefined"||meatTypeInput==null||meatTypeInput===""){
        alert("肉的类别不可以为空");
        return;
    }
    if(meatTypeInput!="undefined"&&meatTypeInput!=""&&meatTypeInput!=null) {
        parm = parm + "&meatType=" + meatTypeInput;
    }

    //获取供应详情
    var purchasingInfoInput =$("#information #purchasing-information .fill-information .purchasing-info-input").val();

    if(purchasingInfoInput=="undefined"||purchasingInfoInput==null||purchasingInfoInput===""){
        alert("采购详情不可以为空");
        return;
    }

    if(purchasingInfoInput!="undefined"&&purchasingInfoInput!=""&&purchasingInfoInput!=null) {
        parm = parm + "&purchaseDesc=" + encodeURIComponent(purchasingInfoInput);
        
    }


    //获取供应详情图片
    var purchasingImage =$("#information #purchasing-information .fill-information .purchasing-info-input").data("image");

    if(purchasingImage!="undefined"&&purchasingImage!=""&&purchasingImage!=null) {
        console.log(purchasingImage.join(","));
        parm = parm + "&imageUrl=" + purchasingImage.join(",");
    }

    //获取肉的类别
    var meatCategoryInput =$("#information #purchasing-information .fill-information .meat-category-input").data("value");
    if(meatCategoryInput!="undefined"&&meatCategoryInput!=""&&meatCategoryInput!=null) {
        parm = parm + "&meatCategoryId=" + meatCategoryInput;
    }

    //获取肉数量
    var meatNumInput =$("#information #purchasing-information .fill-information .meat-num-input").val();
    if(meatNumInput!="undefined"&&meatNumInput!=""&&meatNumInput!=null) {
        parm = parm + "&amount=" + meatNumInput;
    }

    //获取肉单位
    var meatunitInput =$("#information #purchasing-information .fill-information .meat-unit-input").val();
    if(meatunitInput!="undefined"&&meatunitInput!=""&&meatunitInput!=null) {
        parm = parm + "&unit=" + meatunitInput;
    }
    
	//获取单价
	var meatPriceInput =$("#information #purchasing-information .fill-information .meat-price-input").val();
    if(meatPriceInput!="undefined"&&meatPriceInput!=""&&meatPriceInput!=null) {
        parm = parm + "&price=" + meatPriceInput;
    }


    //获取交货地
    var deliveryArea =$("#information #purchasing-information .fill-information .delivery-area").val();
    if(deliveryArea!="undefined"&&deliveryArea!=""&&deliveryArea!=null) {
        parm = parm + "&deliveryArea=" + deliveryArea;
    }

    //获取产地
    var purchasingPlaceInput =$("#information #purchasing-information .fill-information .purchasing-place-input").data("value");
    if(purchasingPlaceInput!="undefined"&&purchasingPlaceInput!=""&&purchasingPlaceInput!=null) {
        parm = parm + "&countryId=" + purchasingPlaceInput;
    }


    //获取失效期
    var failurePeriodInput =$("#information #purchasing-information .fill-information .failure-period-input").val();
    if(failurePeriodInput!="undefined"&&failurePeriodInput!=""&&failurePeriodInput!=null) {
        parm = parm + "&invalidDate=" + failurePeriodInput;
    }


    //获取保质期
    var qualityGuaranteePeriod =$("#information #purchasing-information .fill-information .quality-guarantee-period").val();
    if(qualityGuaranteePeriod!="undefined"&&qualityGuaranteePeriod!=""&&qualityGuaranteePeriod!=null) {
        parm = parm + "&shelfLife=" + qualityGuaranteePeriod;
    }




    //获取姓名
    var name =$("#information #purchasing-information .seller-information .name").val();
    if(name!="undefined"&&name!=""&&name!=null) {
        parm = parm + "&contact=" + name;
    }

    //获取手机号
    var phone =$("#information #purchasing-information .seller-information .phone").val();
    if(phone!="undefined"&&phone!=""&&phone!=null) {
        parm = parm + "&phone=" + phone;
    }

    //获取QQ
    var QQ =$("#information #purchasing-information .seller-information .QQ").val();
    if(QQ!="undefined"&&QQ!=""&&QQ!=null) {
        parm = parm + "&qq=" + QQ;
    }
    //获取微信
    var weChat =$("#information #purchasing-information .seller-information .WeChat").val();
    if(weChat!="undefined"&&weChat!=""&&weChat!=null) {
        parm = parm + "&weChat=" + weChat;
    }
	parm=parm +"&" + Infornation;
    console.log(parm)
    // $.ajax({
    //     url:host + num,
    //     data:parm,
    //     type:"get",
    //     success:function(res){
		// 	console.log(res);
		// 	console.log(parm)
    //         if(res.success == true){
    //             self.location="message.html";
    //             deletePurchasingInfo();
    //         }else{
    //             //就是无数据则返回什么
		// 		alert(res.message)
    //         }
    //     },
    //     error:function () {
    //     	alert(res.message)
    //         return null;
    //     }
    // });
}
function deletePurchasingInfo(){
    //获取肉的类别
    $("#information #purchasing-information .fill-information .meat-type-input").data("value","");
    $("#information #purchasing-information .fill-information .meat-type-input").val("");

    //获取供应详情
    $("#information #purchasing-information .fill-information .purchasing-info-input").val("");
    $(".purchasing-info .option-content>.txt>.content").val("")

    //获取肉的类别
   $("#information #purchasing-information .fill-information .meat-category-input").data("value","");
   $("#information #purchasing-information .fill-information .meat-category-input").val("");

    //获取肉数量
    $("#information #purchasing-information .fill-information .meat-num-input").val("");

    //获取肉单位
    $("#information #purchasing-information .fill-information .meat-unit-input").val("");
    
    //获取肉的价格
    $("#information #purchasing-information .fill-information .meat-price-input").val("");

    //获取交货地
    $("#information #purchasing-information .fill-information .delivery-area").val("");

    //获取产地
    $("#information #purchasing-information .fill-information .purchasing-place-input").data("value");
    $("#information #purchasing-information .fill-information .purchasing-place-input").data("value","");

    //获取失效期
    $("#information #purchasing-information .fill-information .failure-period-input").val("");

    //获取保质期
    $("#information #purchasing-information .fill-information .quality-guarantee-period").val("");

    //获取姓名
    $("#information #purchasing-information .seller-information .name").val("");

    //获取手机号
    $("#information #purchasing-information .seller-information .phone").val("");

    //获取QQ
    $("#information #purchasing-information .seller-information .QQ").val("");

    //获取微信
    $("#information #purchasing-information .seller-information .WeChat").val("");

}




//获取供货类型的函数
function askprovideType(num){
    $.ajax({
        url:host+num,
        type : "get",
        success:function(res){
            if(res.success == true){

                for(var i=0;i<res.data.length;i++){
                    $(".supply-type>.modal-dialog>.modal-content>.option-content").prepend("<div data-value=\""+res.data[i].id+"\">"+res.data[i].provideWay+"</div>");
                    //供应选择肉的类别
                    $(".supply-type>.modal-dialog>.modal-content>.option-content>div").click(function(){

                        $("#supply-information>.fill-information .row .supply-type-input").val($(this).html());
                        $("#supply-information>.fill-information .row .supply-type-input").data("value",$(this).data("value"));
                        $('.supply-type').modal('hide');
                    });
                }
            }else{
            }
        },
        error:function () {
        }
    });
}


//获取包装规格
function askpacking(num){
    $.ajax({
        url:host+num,
        type : "get",
        success:function(res){
        	
            if(res.success == true){
                for(var i=0;i<res.data.length;i++){
                    $(".supply-packing>.modal-dialog>.modal-content>.option-content").prepend("<div data-value='"+res.data[i].id+"'>"+res.data[i].packing+"</div>");
                    //供应选择肉的类别
                    $(".supply-packing>.modal-dialog>.modal-content>.option-content>div").click(function(){
                        $('.supply-packing').modal('hide');

                        $("#supply-information>.fill-information .row .supply-packing-input").val($(this).html());
                        $("#supply-information>.fill-information .row .supply-packing-input").data("value",$(this).data("value"));
                    });
                }
            }else{
            }
        },
        error:function () {
        }
    });
}


//获取g供应肉的分类
function getSupplyMeatCategoryInfos(num){

    $.ajax({
        url:host+num,
        type : "get",
        success:function(res){
        	
            if(res.success == true){

                $(".supply-meat-category>.modal-dialog>.modal-content>.option-content").empty();

                for(var i=0;i<res.data.length;i++){
                    //填充供应消息
                    $(".supply-meat-category>.modal-dialog>.modal-content>.option-content").prepend("<div data-value=\""+res.data[i].id+"\">"+res.data[i].meatName+"</div>");
                    //填充采购消息


                }
                $(".supply-meat-category>.modal-dialog>.modal-content>.option-content>div").click(function(){
                    $('.supply-meat-category').modal('hide');

                    var supplyMeatCategoryData= $("#supply-information>.fill-information .row .meat-category-input").val()+","+$(this).html();
                    if (supplyMeatCategoryData.substr(0,1)==',') supplyMeatCategoryData=supplyMeatCategoryData.substr(1);
                    $("#supply-information>.fill-information .row .meat-category-input").val(unique(supplyMeatCategoryData.split(",")).toString());




                    var supplyMeatCategoryDatavalue = $("#supply-information>.fill-information .row .meat-category-input").data("value");
                    if(supplyMeatCategoryDatavalue==undefined){
                        supplyMeatCategoryDatavalue="";
                    }
                    supplyMeatCategoryDatavalue =supplyMeatCategoryDatavalue+","+$(this).data("value");

                    if (supplyMeatCategoryDatavalue.substr(0,1)==',') supplyMeatCategoryDatavalue=supplyMeatCategoryDatavalue.substr(1);
                    console.log(unique(supplyMeatCategoryDatavalue.split(",")).toString());
                    $("#supply-information>.fill-information .row .meat-category-input").data("value",unique(supplyMeatCategoryDatavalue.split(",")).toString());

                });




            }else{
            }
        },
        error:function () {
        }
    });
}

//获取采购肉的分类
function getPurchasingMeatCategoryInfos(num){
    $.ajax({
        url:host+num,
        type : "get",
        success:function(res){
            if(res.success == true){
                $(".purchasing-meat-category>.modal-dialog>.modal-content>.option-content").empty();
                for(var i=0;i<res.data.length;i++){

                    //填充采购消息
                    $(".purchasing-meat-category>.modal-dialog>.modal-content>.option-content").prepend("<div data-value=\""+res.data[i].id+"\">"+res.data[i].meatName+"</div>");
                    //供应选择肉的类别

                }



                //供应选择肉的类别
                $(".purchasing-meat-category>.modal-dialog>.modal-content>.option-content>div").click(function(){
                    $('.purchasing-meat-category').modal('hide');

                    var purchasingMeatCategoryData= $("#purchasing-information>.fill-information .row .meat-category-input").val()+","+$(this).html();
                    if (purchasingMeatCategoryData.substr(0,1)==',') purchasingMeatCategoryData=purchasingMeatCategoryData.substr(1);
                    $("#purchasing-information>.fill-information .row .meat-category-input").val(unique(purchasingMeatCategoryData.split(",")).toString());


                    var purchasingMeatCategoryDatavalue = $("#purchasing-information>.fill-information .row .meat-category-input").data("value");
                    if(purchasingMeatCategoryDatavalue==undefined){
                        purchasingMeatCategoryDatavalue="";
                    }
                    purchasingMeatCategoryDatavalue =purchasingMeatCategoryDatavalue+","+$(this).data("value");
                    if (purchasingMeatCategoryDatavalue.substr(0,1)==',') purchasingMeatCategoryDatavalue=purchasingMeatCategoryDatavalue.substr(1);
                    console.log(unique(purchasingMeatCategoryDatavalue.split(",")).toString());
                    $("#purchasing-information>.fill-information .row .meat-category-input").data("value",unique(purchasingMeatCategoryDatavalue.split(",")).toString());
                });

            }else{
            }
        },
        error:function () {
        }
    });
}

//数组去重
 function unique(arr)
{
    var n = []; //一个新的临时数组
    for(var i = 0; i < arr.length; i++) //遍历当前数组
    {
        //如果当前数组的第i已经保存进了临时数组，那么跳过，
        //否则把当前项push到临时数组里面
        if (n.indexOf(arr[i]) == -1) n.push(arr[i]);
    }
    return n;
}
//获取产地
function getProvidePlace(num){
    $.ajax({
        url:host+num,
        type : "get",
        success:function(res){
            if(res.success == true){
                for(var i=0;i<res.data.length;i++){

                    //填充供应消息
                    $(".supply-place>.modal-dialog>.modal-content>.option-content").prepend("<div data-value='"+res.data[i].id+"'>"+res.data[i].countryName+"</div>");
                    $(".purchasing-place>.modal-dialog>.modal-content>.option-content").prepend("<div data-value='"+res.data[i].id+"'>"+res.data[i].countryName+"</div>");


                    //产地选择
                    $(".supply-place>.modal-dialog>.modal-content>.option-content>div").click(function(){
                        $('.supply-place').modal('hide');
                        $("#supply-information>.fill-information .row .supply-place-input").val($(this).html());
                        $("#supply-information>.fill-information .row .supply-place-input").data("value",$(this).data("value"));

                    });


                    //产地选择
                    $(".purchasing-place>.modal-dialog>.modal-content>.option-content>div").click(function(){
                        $('.purchasing-place').modal('hide');
                        $("#purchasing-information>.fill-information .row .purchasing-place-input").val($(this).html());
                        $("#purchasing-information>.fill-information .row .purchasing-place-input").data("value",$(this).data("value"));

                    });

                    /*
                    //填充采购消息
                    $(".purchasing-meat-category>.modal-dialog>.modal-content>.option-content").prepend("<div>"+res.data[i].meatName+"</div>");




                    //供应选择肉的类别
                    $(".purchasing-meat-category>.modal-dialog>.modal-content>.option-content>div").click(function(){
                        $('.purchasing-meat-category').modal('hide');
                        $("#purchasing-information>.fill-information .row .meat-category-input").val($(this).html());
                    });
                     */
                }

            }else{
            }
        },
        error:function () {
        }
    });
}

$('body').on('touchmove', function (e) {
    if($('.supply-info').hasClass('in')){
        return false
    }
})




//上传图片
var imgList = [];
var $upInput = $('.uploadImg');
var changeInput = false;
$('#supply-upload-photo').on('change', function () {
    changeInput = true;
    var files;
    if (!this.files.length) return;
    files = Array.prototype.slice.call(this.files);
    if(files.length + imgList.length > 5){
        lay.msg('最多可以同时上传五张照片')
        return;
    }
    if(files.length + imgList.length === 5){
        $upInput.hide();
    }else{
        $upInput.show();
    }
    lay.msg('正在进行图片压缩')
    files.forEach(function (file) {
        lrz(file,{
            quality: 0.5,
            fieldName: 'imageFile',
            width: 880
        })
            .then(function (rst) {
                // 处理成功会执行
                console.log(rst)
                imgList.push(rst)
                $('<span><i data-img='+(imgList.length-1)+'></i><div><img src="'+rst.base64+'" alt=""></div></span>').insertBefore('.uploadImg');
                $('.img-container span').css('height',$('.img-container span').width());
                $('.supply-info>.modal-dialog>.modal-content .uploadImg').css('height', $('.img-container span').width())
            })
            .catch(function (err) {
                // 处理失败会执行
                console.log(err)
                lay.msg('压缩图片失败，请刷新后重试')
            })
            .always(function () {
                // 不管是成功失败，都会执行
            });
    })
    // console.log(imgList)
})
$('.img-container').on('click', function (e) {
    var target = e.target,
        imgUrl = e.target.src?e.target.src:'',
        count;
    if (target.nodeName==="IMG") {
        $('#imgbox').empty().append('<div><i></i><img src='+imgUrl+'></div>').show()
    }else if(target.nodeName==="I"){
        layer.open({
            content: '确认删除照片吗？'
            ,btn: ['取消', '确定']
            ,style: 'color:#333;'
            ,skin: 'footer'
            ,no: function(){
                layer.closeAll();
                count = $(target).data('img');
                imgList.splice(count,1)
                supplyPhoto.splice(count,1)
                $(target).parent('span').fadeOut(500,function () {
                    if(imgList.length < 5) {
                        $upInput.show();
                    }
                    $(this).remove()
                    $('.img-container').find('i').each(function () {
                        $(this).attr('data-img',$(this).index('i'))
                    })
                })
            }
        });


    }
})
$('#imgbox').click(function () {
    $(this).hide();
})
// 图片上传
$('.true button').click(function (e) {
    if(changeInput){
        supplyPhoto = [];
        var index = 0
        imgList.forEach(function (rst,num) {
            $.ajax({
                url:hostImgUpload,
                type:"post",
                data:rst.formData,
                processData:false,// 不要处理发送的数据
                contentType:false,// 不要设置请求头
                dataType:'json',
                beforeSend:function () {
                    layer.open({
                        type: 2
                        ,content: '图片上传中...'
                        ,shadeClose:false
                    });
                },
                success:function(data){
                    changeInput = false;
                    index++;
                    supplyPhoto.push(data.message)
                    if(index === imgList.length){
                        layer.closeAll();
                        var supplyInfoTxt =  $(".supply-info .option-content>.txt>.content").val();
                        $("#supply-information>.fill-information .row .provide-info-input ").val(supplyInfoTxt);
                        $("#supply-information>.fill-information .row .provide-info-input ").attr("data-image",supplyPhoto);

                        $('.supply-info').modal('hide');
                        lay.msg('图片上传完毕，祝您工作愉快')
                    }

                },
                error:function(e){
                    alert("错误！！");
                    // window.clearInterval(timer);
                }
            });
        })
    }else {
        var supplyInfoTxt =  $(".supply-info .option-content>.txt>.content").val();

        $("#supply-information>.fill-information .row .provide-info-input ").val(supplyInfoTxt);
        $("#supply-information>.fill-information .row .provide-info-input ").data("image",supplyPhoto);

        $('.supply-info').modal('hide');
    }


    
})
// $("#supply-upload-photo").fileinput({
//     uploadUrl: hostImgUpload, // you must set a valid URL here else you will get an error
//     overwriteInitial: false,
//     maxFileSize: 1024*1024*1024,
//     maxFilesNum: 5,
//     allowedFileExtensions : ['jpg', 'png','gif','jpeg'],
//     //allowedFileTypes: ['image', 'video', 'flash'],
//     slugCallback: function(filename) {
//         supplyPhoto=[];
//         return filename.replace('(', '_').replace(']', '_');
//     },
//     maxFileCount:5,
//     msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
// });
//
//
//
// $("#supply-upload-photo").on("fileuploaded", function (event, data, previewId, index) {
//     supplyPhoto.push(data.response.message);
// });
// $("#supply-upload-photo").on("filepreupload", function (event, data, previewId, index) {
//     console.log(data)
// });
//
//
//
//
// // $("#purchasing-upload-photo").fileinput({
// //     uploadUrl: hostImgUpload, // you must set a valid URL here else you will get an error
// //     overwriteInitial: false,
// //     maxFileSize: 1024*1024*1024,
// //     maxFilesNum: 5,
// //     allowedFileExtensions : ['jpg', 'png','gif'],
// //     //allowedFileTypes: ['image', 'video', 'flash'],
// //     slugCallback: function(filename) {
// //         purchasingPhoto=[];
// //         return filename.replace('(', '_').replace(']', '_');
// //     }
// // });
// //
// //
// //
// // $("#purchasing-upload-photo").on("fileuploaded", function (event, data, previewId, index) {
// //     purchasingPhoto.push(data.response.message);
// // });
//
// $('.file-drop-zone-title').html('请最多选择五张图片 …')
//
//
//
// //上传图片结束
//
// // 图片预览
// var imgList = [];
// $('#supply-upload-photo').on('change', function () {
//     if(imgList.length >= 5){
//         alert('最多只能上传五张')
//     }
//     ([]).forEach.call(this.files,function (val) {
//         if(val){
//             var url = URL.createObjectURL(val)
//             imgList.push(url)
//         }
//     })
//     console.log(imgList)
//
// })
//
// $('.file-preview-thumbnails').on('click', function (e) {
//     var index,targget = e.target;
//     // var imgArr = [];
//     if(targget.nodeName==="IMG"){
//         index = $(targget).parent().data('fileindex')
//         console.log(index)
//         $('#imgbox').empty().append('<img src='+imgList[index]+'>').show()
//         // $(this).find('img').each(function () {
//         //     imgArr.push(this.src)
//         // })
//
//     }else if(targget.className.indexOf('kv-file-remove') !==-1 || targget.className.indexOf('glyphicon-trash') !==-1){
//         if(targget.nodeName === 'I'){
//             index = $(targget).parent().parent().parent().parent().parent().data('fileindex')
//             console.log($(targget).parent().parent().parent().parent().parent().data('fileindex'))
//         }else {
//             index = $(targget).parent().parent().parent().parent().data('fileindex')
//             console.log($(targget).parent().parent().parent().parent().data('fileindex'))
//         }
//         imgList[index] = null;
//         console.log(imgList)
//     }
// })
// $('#imgbox').click(function () {
//     $(this).hide()
// })
